//
//  RowImageLabelCell.m
//  
//  jhf
//
//  Created by 计海峰 on 2020/8/7.
//

#import "RowImageLabelCell.h"

#import "RowImageLabelCell.h"

#import "RowTitleBaseM.h"

@implementation RowImageLabelCell

- (void)setup {
    [self.bgV addSubview:self.titleLB];
    [self.bgV addSubview:self.subTitleLB];
    [self.bgV addSubview:self.needLB];
    [self.bgV addSubview:self.rightLB];
    [self.bgV addSubview:self.unitLB];
    [self.bgV addSubview:self.rightIMG];
    [self.bgV addSubview:self.rightLeftIMG];
    [self.bgV addSubview:self.leftIMG];
    
    [self.titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bgV);
        make.left.equalTo(self.bgV).offset(15);
        make.top.greaterThanOrEqualTo(self.bgV).offset(15);
    }];
    
    [self.leftIMG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bgV);
        make.left.equalTo(self.bgV).offset(15);
    }];
    
    [self.needLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLB);
        make.left.equalTo(self.titleLB.mas_right).offset(2);
    }];
    
    [self.subTitleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLB);
        make.left.equalTo(self.titleLB.mas_right).offset(4);
    }];
    
    [self.rightLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLB);
        make.right.equalTo(self.bgV).offset(-15);
        make.left.equalTo(self.subTitleLB.mas_right).offset(0);
    }];
    
    [self.rightIMG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLB);
        make.right.equalTo(self.bgV).offset(-15);
    }];
    
    [self.rightLeftIMG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLB);
        make.right.equalTo(self.rightIMG.mas_left).offset(-8);
    }];
    
    [self.unitLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLB);
        make.right.equalTo(self.bgV).offset(-15);
    }];
    
}

- (void)bindTo:(__kindof id<BindProtocol>)model; {
    [super bindTo:model];
    BindModel *bind = (BindModel *)model;
    RowTitleBaseM *config = (RowTitleBaseM *)bind.model;
    
    if (config.titleLBControl) { /// 标题
        [config.titleLBControl controlLB:self.titleLB];
        
        // 顶部
        CGFloat topCons = config.titleLBControl.vTopCons ?: 15;
        // 左侧
        CGFloat leftCons = config.titleLBControl.vLeftCons ?: 15;
        [self.titleLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgV).offset(topCons);
            make.left.equalTo(self.bgV).offset(leftCons);
            if (config.titleLBControl.vRightCons) {
                make.right.equalTo(self.bgV.mas_right).mas_offset(-config.titleLBControl.vRightCons);
            }
            if (config.titleLBControl.vWidthCons) {
                make.width.mas_offset(config.titleLBControl.vWidthCons);
            }
        }];
    }
    
    if (config.rightLBControl) { /// 右侧显示
        [config.rightLBControl controlLB:self.rightLB];
        CGFloat rightCons = config.rightLBControl.vRightCons ? -config.rightLBControl.vRightCons : -15;
        [self.rightLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.bgV).offset(rightCons);
        }];
        if (config.rightLBControl.vTopCons) {
            [self.rightLB mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.greaterThanOrEqualTo(self.bgV).offset(config.rightLBControl.vTopCons);
            }];
        }
        if (config.rightLBControl.vLeftCons) {
            [self.rightLB mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.lessThanOrEqualTo(self.bgV).offset(config.rightLBControl.vLeftCons);
            }];
        }
        if (!config.rightLBControl.textStr || config.rightLBControl.textStr.length <= 0) {
            if (config.placeholderStr) {
                self.rightLB.text = config.placeholderStr;
                self.rightLB.textColor = [UIColor colorWithHexString:config.placeholderColorHex];
                self.rightLB.font = config.placeholderFont;
            }
        }
    }
    
    if (config.subTitleLBControl) { /// 副标题
        [config.subTitleLBControl controlLB:self.subTitleLB];
        if (config.subTitleLBControl.vRightCons) {
            [self.subTitleLB mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.lessThanOrEqualTo(self.bgV).offset(config.subTitleLBControl.vRightCons);
            }];
        }
        if (config.subTitleLBControl.vCenterYCons) {
            [self.subTitleLB mas_updateConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.titleLB).offset(config.subTitleLBControl.vCenterYCons);
            }];
        }
    }
    
    // 必须型号显示
    if (config.needLBControl) {  /// * 必传
        [config.needLBControl controlLB:self.needLB];
        if (config.needLBControl.vLocation > 0) {
            if (config.needLBControl.vLocation == 1) { /// 左侧
                CGFloat leftCons = config.needLBControl.vRightCons ?: 2;
                [self.needLB mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(self.titleLB);
                    make.right.equalTo(self.titleLB.mas_left).offset(-leftCons);
                }];
            }else if (config.needLBControl.vLocation == 3) { /// 右侧
                CGFloat rightCons = config.needLBControl.vLeftCons ?: 2;
                [self.needLB mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(self.titleLB);
                    make.left.equalTo(self.titleLB.mas_right).offset(rightCons);
                }];
            }
        }
    }
    self.needLB.hidden = !config.needLBControl;
    
    if (config.unitLBControl) { /// 单位
        [config.unitLBControl controlLB:self.unitLB];
        CGFloat rightCons = config.unitLBControl.vRightCons ? -config.unitLBControl.vRightCons : -15;
        [self.unitLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.bgV).offset(rightCons);
            if (config.unitLBControl.vWidthCons > 0) {
                make.width.mas_equalTo(config.unitLBControl.vWidthCons);
            }else {
                make.width.mas_equalTo(config.unitLBControl.labelWidth);
            }
        }];
    }else {
        self.unitLB.text = @"";
        [self.unitLB mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.bgV).offset(-15);
            make.width.mas_equalTo(0);
        }];
    }
    
    // 图片
    self.rightIMG.hidden = true;
    self.rightLeftIMG.hidden = true;
    self.leftIMG.hidden = true;
    
    /// 右侧图片约束
    if (config.rightImgControl) {
        self.rightIMG.hidden = false;
        [config.rightImgControl controlIMG:self.rightIMG blockBool:nil];
        CGFloat rightCons = config.rightImgControl.vRightCons ? -config.rightImgControl.vRightCons : -15;
        [self.rightIMG mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.bgV).offset(rightCons);
        }];
        if (config.rightImgControl.vWidthCons && config.rightImgControl.vHeightCons) {
            CGFloat heightCons = config.rightImgControl.vHeightCons;
            CGFloat widthCons = config.rightImgControl.vWidthCons;
            [self.rightIMG mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(heightCons);
                make.width.mas_equalTo(widthCons);
            }];
        }
    }
    
    if (config.rightLeftImgControl) {
        self.rightLeftIMG.hidden = false;
        [config.rightLeftImgControl controlIMG:self.rightLeftIMG blockBool:nil];
        CGFloat rightCons = config.rightLeftImgControl.vRightCons ? -config.rightLeftImgControl.vRightCons : -8;
        [self.rightLeftIMG mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.rightIMG.mas_left).offset(rightCons);
        }];
        if (config.rightLeftImgControl.vWidthCons && config.rightLeftImgControl.vHeightCons) {
            CGFloat heightCons = config.rightLeftImgControl.vHeightCons;
            CGFloat widthCons = config.rightLeftImgControl.vWidthCons;
            [self.rightLeftIMG mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(heightCons);
                make.width.mas_equalTo(widthCons);
            }];
        }
    }
    
    /// 左侧图片约束
    if (config.leftImgControl) {
        self.leftIMG.hidden = false;
        [config.leftImgControl controlIMG:self.leftIMG blockBool:nil];
        // 左侧
        CGFloat leftCons = config.leftImgControl.vLeftCons ?: 15;
        [self.leftIMG mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bgV).offset(leftCons);
        }];
        if (config.leftImgControl.vWidthCons && config.leftImgControl.vHeightCons) {
            CGFloat heightCons = config.leftImgControl.vHeightCons;
            CGFloat widthCons = config.leftImgControl.vWidthCons;
            [self.leftIMG mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(heightCons);
                make.width.mas_equalTo(widthCons);
            }];
        }
    }
    
    //
    //    /// 标题设置 没有 输入框左移动
    //    if (config.titleLBControl.textLeftCons.clearSpace.length == 0) {
    //        self.titleLBWidthCons.constant = 0;
    //    }else {
    //        self.titleLBWidthCons.constant = 72;
    //    }
    
    // 初始化默认参数 用于修改什么的
    if (config.dataKey && config.dataKey.length > 0 && config.keyValue && config.keyValue.length > 0) {
//        NSMutableDictionary *tempParam = [NSMutableDictionary dictionary];
//        [tempParam setValue:config.keyValue forKey:config.dataKey];
        [bind.data setValue:config.keyValue forKey:config.dataKey];
    }
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

//MARK: - lazy
- (UILabel *)rightLB {
    if (!_rightLB) {
        _rightLB = [UILabel new];
    }
    return _rightLB;
}


- (UILabel *)subTitleLB {
    if (!_subTitleLB) {
        _subTitleLB = [UILabel new];
    }
    return _subTitleLB;
}

- (UILabel *)titleLB {
    if (!_titleLB) {
        _titleLB = [UILabel new];
    }
    return _titleLB;
}

- (UIImageView *)rightIMG {
    if (!_rightIMG) {
        _rightIMG = [[UIImageView alloc] init];
    }
    return _rightIMG;
}

- (UIImageView *)rightLeftIMG {
    if (!_rightLeftIMG) {
        _rightLeftIMG = [[UIImageView alloc] init];
    }
    return _rightLeftIMG;
}

- (UIImageView *)leftIMG {
    if (!_leftIMG) {
        _leftIMG = [[UIImageView alloc] init];
    }
    return _leftIMG;
}

- (UILabel *)needLB {
    if (!_needLB) {
        _needLB = [UILabel new];
        _needLB.backgroundColor = [UIColor redColor];
        _needLB.text = @"*";
    }
    return _needLB;
}

- (UILabel *)unitLB {
    if (!_unitLB) {
        _unitLB = [UILabel new];
    }
    return _unitLB;
}

@end
